/********************************************************************
 
 Copyright (C), 2012年 dhc. All rights reserved.
 
 Filename: 	    MailMsgReceiverView.m
 
 Author:        muzongcun
 
 Version:       1.0
 
 Description:   MailMsgReceiverView
 
 History:
 
 muzongcun    2012/3/10     1.0     Create
 
 *********************************************************************/

#import "MailMsgReceiverView.h"
#import "Type.h"
@implementation MailMsgReceiverView
@synthesize delegate, recid;

- (id)initWithFrame:(CGRect)frame 
{
	if ((self = [super initWithFrame:frame])) 
	{
		[self addSubview:nameBtn];
		[self addSubview:deleteImgV];
		self.backgroundColor = [UIColor clearColor];
	}
	return self;
}

-(id)initWithObject:(NSDictionary*)dict
{
	NSString *reciverID = [dict objectForKey:@"t_1_mail"];
	NSString *nameStr = [dict objectForKey:@"t_1_contact_name"];
	if(nameStr == nil || reciverID == nil)
		return [self initWithFrame:CGRectZero];
	
	recid = [reciverID retain];
	CGSize titlesize = [nameStr sizeWithFont:[UIFont systemFontOfSize:15.0] forWidth:150 lineBreakMode:UILineBreakModeWordWrap];
	
	//nameBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    UIImage* contactNameImage = nil;
    NSString* devVersion = [[UIDevice currentDevice] systemVersion];
    if ([devVersion compare:@"4.3.3"] == 0)
    {
        contactNameImage = [UIImage imageNamed:PIC_MAIL_CONTACE_NAME_BG];
    }else
    {
        contactNameImage = [[UIImage imageNamed:PIC_MAIL_CONTACE_NAME_BG] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 15, 15, 25)]; 
    }

    nameBtn = [[UIButton alloc]init ];
    [nameBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    nameBtn.titleLabel.font = [UIFont systemFontOfSize: 15.0];
	[nameBtn setTitle:nameStr forState:UIControlStateNormal];
	nameBtn.userInteractionEnabled = NO;
	nameBtn.frame = CGRectMake(0, 0, titlesize.width + 10, titlesize.height + 4);
    
    [nameBtn setBackgroundImage:contactNameImage forState:UIControlStateNormal];
    [nameBtn setBackgroundImage:contactNameImage forState:UIControlStateHighlighted];
	
	deleteImgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:PIC_MAIL_DEL_CONTACTOR]];
	deleteImgV.frame = CGRectMake(nameBtn.frame.size.width, 0.0, 25, 25);
	
	return [self initWithFrame:CGRectMake(0, 0, nameBtn.frame.size.width + 25.0, 30.0)];
}

- (void)drawRect:(CGRect)rect 
{
	[super drawRect:rect];
}

- (void)dealloc 
{
	[recid release];
	[deleteImgV release];
    [super dealloc];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	UITouch *touch = [touches anyObject];
	CGPoint p = [touch locationInView:self];
	
	if(p.x > self.frame.size.width - 30)
	{
		[delegate receiveViewClicked:self];
	}
}

@end
